package com.leil.project

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Star
import androidx.compose.material.icons.outlined.CalendarToday
import androidx.compose.material.icons.outlined.KeyboardArrowDown
import androidx.compose.material.icons.outlined.KeyboardArrowUp
import androidx.compose.material.icons.outlined.Star
import androidx.compose.material3.Checkbox
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.unit.dp
import org.jetbrains.compose.ui.tooling.preview.Preview

data class Task(
    val id: Int,
    val description: String,
    val groupName: String,
    val deadline: String,
    val isComplete: Boolean,
    val isImportant: Boolean
)


data class TaskGroup(
    val id: Int,
    val name: String,
    val tasks: List<Task>,
    val isExpanded: Boolean
)


@Composable
fun TaskGroupList(groups: List<TaskGroup>) {
    val state = rememberLazyListState()
    Box(
        modifier = Modifier.fillMaxWidth()
    ) {
        LazyColumn(
            state = state,
            modifier = Modifier.fillMaxWidth()
        ) {
            items(groups) { group ->
                Spacer(modifier = Modifier.height(8.dp))
                TaskGroup(group)
                Spacer(modifier = Modifier.height(8.dp))
            }
        }
        PlatformScrollbar(
            state = state,
            modifier = Modifier.fillMaxHeight().align(Alignment.CenterEnd)
        )
    }
}


@Composable
fun TaskGroup(taskGroup: TaskGroup) {
    var visible by remember { mutableStateOf(taskGroup.isExpanded) }
    Column {
        Row(
            modifier = Modifier.clip(RoundedCornerShape(8.dp)).clickable { visible = !visible }.background(MaterialTheme.colorScheme.surface).padding(8.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Icon(
                imageVector = if (visible) Icons.Outlined.KeyboardArrowUp else Icons.Outlined.KeyboardArrowDown,
                contentDescription = "TipArrow"
            )
            Spacer(modifier = Modifier.width(5.dp))
            Text(text = taskGroup.name, style = MaterialTheme.typography.bodyLarge)
            Spacer(modifier = Modifier.width(5.dp))
            Text(text = taskGroup.tasks.size.toString(), style = MaterialTheme.typography.bodyLarge)
        }
        Spacer(modifier = Modifier.height(10.dp))
        AnimatedVisibility(
            visible = visible
        ) {
            Column {
                taskGroup.tasks.forEach { task ->
                    Task(task = task)
                    Spacer(modifier = Modifier.height(6.dp))
                }
            }
        }
    }
}


@Composable
fun Task(task: Task) {
    var checked by remember { mutableStateOf(task.isComplete) }
    Row(
        modifier = Modifier.fillMaxWidth()
            .clip(RoundedCornerShape(8.dp))
            .background(MaterialTheme.colorScheme.surface)
            .padding(vertical = 8.dp, horizontal = 12.dp),
        horizontalArrangement = Arrangement.SpaceBetween,
        verticalAlignment = Alignment.CenterVertically
    ) {
        Row {
            Checkbox(checked = checked, onCheckedChange = { checked = it })
            Column {
                Text(text = task.description, style = MaterialTheme.typography.titleMedium)
                Spacer(modifier = Modifier.height(4.dp))
                Row(verticalAlignment = Alignment.CenterVertically){
                    Spacer(modifier = Modifier.width(8.dp))
                    Text(text = task.groupName, style = MaterialTheme.typography.bodyMedium)
                    Spacer(modifier = Modifier.width(8.dp))
                    Icon(Icons.Outlined.CalendarToday, contentDescription = "Calendar", modifier = Modifier.size(16.dp))
                    Spacer(modifier = Modifier.width(4.dp))
                    Text(text = task.deadline, style = MaterialTheme.typography.bodyMedium)
                }
            }
        }
        Icon(
            imageVector = if (task.isImportant) Icons.Filled.Star else Icons.Outlined.Star,
            contentDescription = "Important",
            modifier = Modifier.size(25.dp)
        )
    }

}


@Preview
@Composable
fun TaskGroupListPreview() {
    val group = createTaskGroup()
    val task = createTask("计划列表")
    Column(
        modifier = Modifier.fillMaxSize().background(MaterialTheme.colorScheme.primaryContainer),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.Start
    ) {
        TaskGroup(group)
    }


}


fun createTaskGroup(): TaskGroup {
    return TaskGroup(
        id = 1,
        name = "默认分组",
        tasks = listOf(
            createTask("默认分组"),
            createTask("默认分组"),
            createTask("默认分组")
        ),
        isExpanded = true
    )
}

fun createTask(groupName: String): Task {
    return Task(
        id = 1,
        description = "每日UI绘制",
        groupName = groupName,
        deadline = "今天",
        isComplete = false,
        isImportant = true
    )
}